package com.wangtao.firstapp.adapters;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.wangtao.firstapp.R;
import com.wangtao.firstapp.inter.OnRVItemClickListener;
import com.wangtao.firstapp.inter.OnRVItemLongClickListener;
import com.wangtao.firstapp.modles.CommunityArticlesData;

import java.util.List;

/**
 * 文章页面中RecyclerView的适配器
 * Created by lanouhn on 16/8/24.
 */
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context context;
    private List<CommunityArticlesData.DataBean.TopicBean> articlesData;
    private OnRVItemClickListener clickListener;
    private OnRVItemLongClickListener longClickListener;

    public void setClickListener(OnRVItemClickListener clickListener) {
        this.clickListener = clickListener;
    }

    public void setLongClickListener(OnRVItemLongClickListener longClickListener) {
        this.longClickListener = longClickListener;
    }

    public RecyclerViewAdapter(Context context, List<CommunityArticlesData.DataBean.TopicBean> articlesData) {
        this.context = context;
        this.articlesData = articlesData;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view_item = LayoutInflater.from(context).inflate(R.layout.item_article_recyclerview, null);
        ItemViewHolder itemholder = new ItemViewHolder(view_item);
        return itemholder;
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ItemViewHolder) {
            List<CommunityArticlesData.DataBean.TopicBean.PicsBean> pics = articlesData.get(position).getPics();
            Picasso.with(context).load(pics.get(0).getUrl()).config(Bitmap.Config.RGB_565).into(((ItemViewHolder) holder).iv_picture_item_article_recyclerview);
            ((ItemViewHolder) holder).tv_title_item_article_recyclerview.setText(articlesData.get(position).getTitle());
            ((ItemViewHolder) holder).tv_browses_item_article_recyclerview.setText("浏览  " + articlesData.get(position).getViews());
            if ("".equals(articlesData.get(position).getComments())) {
                ((ItemViewHolder) holder).tv_comment_item_article_recyclerview.setText("评论  0");
            } else {
                ((ItemViewHolder) holder).tv_comment_item_article_recyclerview.setText("评论  " + articlesData.get(position).getComments());
            }
            Picasso.with(context).load(articlesData.get(position).getUser().getAvatar()).config(Bitmap.Config.RGB_565).into(((ItemViewHolder) holder).iv_smallheader_item_article_recycleview);
            ((ItemViewHolder) holder).tv_username_item_article_recyclerview.setText(articlesData.get(position).getUser().getNickname());
            ((ItemViewHolder) holder).tv_timebefore_item_article_recyclerview.setText(articlesData.get(position).getOrder_time_str());
        }

        if (clickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int clickposition = holder.getLayoutPosition();
                    clickListener.OnRVItemClickListener(clickposition);
                }
            });
        }
        if (longClickListener != null) {
            holder.itemView.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    if (event.getAction() == MotionEvent.ACTION_UP) {
                        int clickposition = holder.getLayoutPosition();
                        longClickListener.OnRVItemLongClickListener(clickposition);
                    }
                    return true;
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return articlesData.size();
    }

    //  item的ViewHolder
    public class ItemViewHolder extends RecyclerView.ViewHolder {
        private ImageView iv_picture_item_article_recyclerview;// RecyclerView的item布局中的大图片
        private ImageView iv_smallheader_item_article_recycleview;// RecyclerView的item布局中的用户小头像
        private TextView tv_title_item_article_recyclerview;// RecyclerView的item布局中的标题
        private TextView tv_browses_item_article_recyclerview;// RecyclerView的item布局中的浏览数
        private TextView tv_comment_item_article_recyclerview;// RecyclerView的item布局中的评论数
        private TextView tv_username_item_article_recyclerview;// RecyclerView的item布局中的用户名
        private TextView tv_timebefore_item_article_recyclerview;// RecyclerView的item布局中的当前时间与信息发布时间的差

        public ItemViewHolder(View itemView) {
            super(itemView);
            iv_picture_item_article_recyclerview = (ImageView) itemView.findViewById(R.id.iv_picture_item_article_recyclerview);
            iv_smallheader_item_article_recycleview = (ImageView) itemView.findViewById(R.id.iv_smallheader_item_article_recycleview);
            tv_title_item_article_recyclerview = (TextView) itemView.findViewById(R.id.tv_title_item_article_recyclerview);
            tv_browses_item_article_recyclerview = (TextView) itemView.findViewById(R.id.tv_browses_item_article_recyclerview);
            tv_comment_item_article_recyclerview = (TextView) itemView.findViewById(R.id.tv_comment_item_article_recyclerview);
            tv_username_item_article_recyclerview = (TextView) itemView.findViewById(R.id.tv_username_item_article_recyclerview);
            tv_timebefore_item_article_recyclerview = (TextView) itemView.findViewById(R.id.tv_timebefore_item_article_recyclerview);
        }
    }
}
